<template>
	<div>
		<el-dialog title="编辑" v-model="isShowDialog" width="600px" @close="closeDialog">
			<el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
				<el-form-item label="组名称" prop="groupName">
					<el-input v-model="formModel.groupName" show-word-limit maxlength="30" placeholder=""
						clearable></el-input>
				</el-form-item>
				<el-form-item label="执行类型" prop="executeType">
					<el-select v-model="formModel.executeType" placeholder="">
						<el-option v-for="item in DictInfo.groupExecuteType" :key="item.key" :label="item.label"
							:value="item.key" />
					</el-select>
				</el-form-item>
				<el-form-item v-if="formModel.executeType === 2" label="间隔时间(S)" prop="delaySeconds"
					:rules="[{ required: true, message: '间隔时间不能为空', trigger: 'change' }]">
					<el-input-number v-model="formModel.delaySeconds" show-word-limit :min="1" :max="999999" placeholder=""
						clearable></el-input-number>
				</el-form-item>
				<el-form-item v-if="formModel.executeType === 1" label="cron表达式" prop="cron"
					:rules="[{ required: true, message: 'cron表达式不能为空', trigger: 'change' }]">
					<el-input v-model="formModel.cron" show-word-limit maxlength="50" placeholder="" clearable></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="formModel.remark" show-word-limit maxlength="400" placeholder=""
						clearable></el-input>
				</el-form-item>
			</el-form>
			<el-divider content-position="left">任务顺序</el-divider>
			<TaskRelation :data="taskList" @onload="onCheckTask"></TaskRelation>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => closeDialog()" size="default">取 消</el-button>
					<el-button type="primary" :loading="submitLoading" @click="onSubmit" size="default">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, inject } from 'vue';
import type { FormInstance } from 'element-plus';
import { TaskGroupInfoEditParamModel, TaskGroupInfoDetailModel, TaskGroupRelationListModel } from '/@/interfaces/DataPipelineModel';
import { handleEdit, handleValidateForm } from '../task-group-info-wrap';
import useObjectUtil from '/@/utils/objectUtil';
import TaskRelation from './task-relation.vue';
import { DictInfo } from '/@/enums/dict-info-enum';

interface State {
	isShowDialog: boolean | true,
	submitLoading: boolean | false,
	formModel: TaskGroupInfoEditParamModel,
	formRules: { [key: string]: any[] },
	taskList: Array<TaskGroupRelationListModel>,
}
export default defineComponent({
	name: 'TaskGroupInfoEdit',
	components: { TaskRelation },
	emits: ['onClose'],
	setup(props, context) {
		const state = reactive<State>({
			isShowDialog: true,
			submitLoading: false,
			formModel: {
				id: undefined,
				groupName: undefined,
				executeType: undefined,
				delaySeconds: undefined,
				cron: undefined,
				remark: undefined,
				taskIdList: [],
			},
			formRules: {
				groupName: [{ required: true, message: '组名称不能为空', trigger: 'change' }],
				cron: [{ required: true, message: 'cron表达式不能为空', trigger: 'change' }]
			},
			taskList: [],
		});
		const methodMap = reactive({
			// 关闭弹窗
			closeDialog: function () {
				state.isShowDialog = false;
				context.emit('onClose');
			},
			// 提交
			onSubmit: async function () {
				state.submitLoading = true;
				setTimeout(() => state.submitLoading = false, 3000);
				if(state.formModel.executeType === 1) {
                    state.formModel.delaySeconds = undefined;
                } else {
                    state.formModel.cron = undefined;
                }
				// 表单验证
				let validate = await handleValidateForm(formRef.value);
				if (!validate) {
					state.submitLoading = false;
					return;
				}
				// 提交
				await handleEdit({ param: state.formModel, pageInfo: undefined }, methodMap.closeDialog);
			},
			onCheckTask: function (ids: string[]) {
				state.formModel.taskIdList = [...ids];
			},
		});
		// 表单赋值
		const detailData: TaskGroupInfoDetailModel | undefined = inject<TaskGroupInfoDetailModel>('detailData');
		if (undefined !== detailData) {
			useObjectUtil.copyProperties(detailData, state.formModel);
			state.taskList = [...detailData.taskList];
			state.formModel.taskIdList = state.taskList.map((item: TaskGroupRelationListModel) => item.taskId as string);
		} else {
			console.error("found undefined for detail");
		}

		const formRef = ref<FormInstance>();
		const executeStatusRef = ref();
		return {
			DictInfo,
			executeStatusRef,
			formRef,
			...toRefs(state),
			...toRefs(methodMap),
		};
	},
});
</script>
